package com.yanqu.road.server.cmd.beautyescort;

import com.yanqu.road.entity.beautyescort.EscortBeautyData;
import com.yanqu.road.entity.beautyescort.rank.EscortWeekUserValue;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.beautyescort.BeautyEscortProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.beautyescort.BeautyEscortModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.manger.beautyescort.BeautyEscortMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;
import java.util.Map;


@Cmd(code = Protocol.S_BEAUTY_ESCORT_RED_DOT_SYNC_FROM_CROSS, desc = "红点-跨服回包")
public class BeautyEscortRedDotCmdFromCross extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BeautyEscortProto.CrossBeautyEscortRedDotRespMsg reqMsg = BeautyEscortProto.CrossBeautyEscortRedDotRespMsg.parseFrom(packet.getBytes());
        List<Integer> escortingList = reqMsg.getBeautyList();
        BeautyEscortProto.BeautyEscortRedDotSyncMsg.Builder respMsg = BeautyEscortProto.BeautyEscortRedDotSyncMsg.newBuilder();
        if(reqMsg.hasUnionReport()){
            respMsg.setUnionReport(reqMsg.getUnionReport());
        }
        if(reqMsg.hasReport()) {
            respMsg.setReport(reqMsg.getReport());
        }
        if(reqMsg.getIdleCart()) {
            int canEscortingCount = 0;
            //美女红点
            BeautyEscortModule beautyEscortModule = player.getModule(BeautyEscortModule.class);
            for (Map.Entry<Integer, UserBeauty> beautyEntry : player.getModule(BeautyModule.class).getBeautyMap().entrySet()) {
                int beautyId = beautyEntry.getKey();
                if (escortingList.contains(beautyId)) {
                    continue;
                }
                //未在走镖
                EscortBeautyData beautyData = beautyEscortModule.getBeautyDataMap().get(beautyId);
                if (beautyData == null || beautyData.getEscortTimes() < GameConfig.ESCORT_TIMES_PER_WEEK) {
                    canEscortingCount++;
                }
            }
            if (canEscortingCount >= 5) {
                respMsg.setEscort(1);
            }else {
                respMsg.setEscort(0);
            }
        }else {
            respMsg.setEscort(0);
        }
        //榜单红点
        EscortWeekUserValue userValue = BeautyEscortMgr.getEscortWeekUserValue(BeautyEscortMgr.getPreWeekId(), player.getUserId());
        if(userValue != null && !userValue.isGetReward() && reqMsg.getMyRank() > 0){
            respMsg.setRank(1);
        }
        player.sendPacket(Protocol.U_BEAUTY_ESCORT_RED_DOT_SYNC, respMsg);
        log.info("{} red dot escort:{}, report:{},{}, rank {}", player.getUserId(), respMsg.getEscort(), respMsg.getReport(), respMsg.getUnionReport(), respMsg.getRank());
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
